package algorithm;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class LeetCode94 {
    // 左 根 右
    public List<Integer> inorderTraversal2(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        dfs(list, root);
        return list;

    }

    public void dfs(List<Integer> list, TreeNode node) {
        if (node == null) {
            return;
        }
        dfs(list, node.left);
        list.add(node.val);
        dfs(list, node.right);
    }


    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();

        while(root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            list.add(root.val);
            root = root.right;
        }
        return list;

    }

}
